package cn.edu.lingnan.services;

import cn.edu.lingnan.dao.StudentMapper;
import cn.edu.lingnan.pojo.Student;
import cn.edu.lingnan.util.MybatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class StudentServiceMysqlImpl implements StudentServices {

    @Override
    public Vector<Student> findAllStudent() throws IOException {
//        String resource = "mybatis-config.xml";
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
//            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//        }
        Vector<Student> vector = null;
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        vector = studentMapper.findAllStudent();
        return vector;
    }

    @Override
    public Student findStudentById(String sid) {
//        String resource = "mybatis-config.xml";
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
//    }
        Student student = null;
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        student = studentMapper.findStudentById(sid);
        return student;
    }

    @Override
    public List<Student> findStudentByNameUseLike(String sname) {
        List<Student> students = null;
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        students = studentMapper.findStudentByNameUseLike(sname);
        sqlSession.close();
        return students;
    }

    @Override
    public int insertStudent(Student student) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        int i = studentMapper.insertStudent(student);
        sqlSession.commit();
        return i;
    }

    @Override
    public int updateStudent(Student student) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        int i = studentMapper.updateStudent(student);
        sqlSession.commit();
        return i;
    }

    @Override
    public int updateStudentPassword(Map map) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        int i = studentMapper.updateStudentPassword(map);
        sqlSession.commit();
        return i;
    }

    @Override
    public int deleteStudent(String sid) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        int i = studentMapper.deleteStudent(sid);
        sqlSession.commit();
        return i;
    }

    @Override
    public List<Student> findStudentUseIf(Map map) {
        List<Student> list = null;
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        list = studentMapper.findStudentUseIf(map);
        return list;
    }

    @Override
    public int updateStudentUseSet(Map map) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        int i= studentMapper.updateStudentUseSet(map);
        sqlSession.commit();
        return i;
    }

    @Override
    public List<Student> findStudentUseForeach(Map map) {
        return null;
    }
}